package guiscreens;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import dao.ExecuteQuery;

/**
 * This class handles deleting and adding ratings in the database. 
 * 
 * @author Lee
 * @version 1.0
 */
public class ItemRatings extends JPanel {

	private CardLayout c1;
	private JPanel basePanel;
	private JFrame thisFrame;
	private JTextField textField;
	private String itemName;
	private String[] rateVals = {"1 Star", "2 Star", "3 Star", "4 Star", "5 Star"};
	private JComboBox rateBox;
	private JLabel lblNewLabel_2;
	private int ratingCounter;

	/**
	 * Create the panel.
	 */
	public ItemRatings(final String itemID, String itemName, final CardLayout c1, final JPanel basePanel, 
			final JFrame thisFrame)
	{
		this.c1 = c1;
		this.basePanel = basePanel;
		this.thisFrame = thisFrame;
		setBackground(Color.WHITE);
		this.itemName = itemName;
		List<String> list = ExecuteQuery.viewRatings(Integer.parseInt(itemID));
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(77dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
				new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(64dlu;default):grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));

		JLabel lblItemId = new JLabel("Item ID");
		add(lblItemId, "2, 2");

		JLabel lblNewLabel = new JLabel(itemID);
		add(lblNewLabel, "4, 2");

		JLabel lblItemName = new JLabel("Item Name");
		add(lblItemName, "2, 4");

		JLabel lblNewLabel_1 = new JLabel(itemName);
		add(lblNewLabel_1, "4, 4");

		JLabel lblAverageRating = new JLabel("Average Rating:");
		add(lblAverageRating, "2, 6");

		lblNewLabel_2 = new JLabel();
		try {
			lblNewLabel_2.setText(list.get(0));

		} catch(Exception e) {
			lblNewLabel_2.setText("No ratings yet");
		}
		add(lblNewLabel_2, "4, 6");

		JLabel lblRatings = new JLabel("Ratings");
		add(lblRatings, "2, 8");

		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		add(panel, "3, 10, 6, 1, fill, fill");
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

		int i = 3;
		while (i > 0)
		{
			try {
				JPanel panelDummy = new JPanel();
				panelDummy.setLayout(new BoxLayout(panelDummy, BoxLayout.Y_AXIS));
				panelDummy.setBackground(Color.WHITE);
				if (list.get(i).equals(login.username)) 
				{
					JLabel delete = new JLabel("<html><u>Delete My Rating</u></html>");
					delete.addMouseListener(new MouseListener()
					{
						public void mouseClicked(MouseEvent e) {
							if (ratingCounter == 0) {
							ExecuteQuery.deleteRating(login.username, itemID);
							JOptionPane.showMessageDialog(null, "Rating deleted! (Refresh your screen)");
							ratingCounter++;
							}
							else JOptionPane.showMessageDialog(null, "You already deleted your rating (refresh your screen)");
						}
						public void mousePressed(MouseEvent e) {}
						public void mouseReleased(MouseEvent e) {}
						public void mouseEntered(MouseEvent e) {}
						public void mouseExited(MouseEvent e) {}
					});
					panelDummy.add(delete);
				}
				JLabel labelDummy = new JLabel(list.get(i));
				JLabel labelDummy2 = new JLabel(list.get(i + 1));
				JLabel labelDummy3 = new JLabel(list.get(i + 2));
				JLabel labelDummy4 = new JLabel(list.get(i + 3));
				panelDummy.add(labelDummy);
				panelDummy.add(labelDummy2);
				panelDummy.add(labelDummy3);
				panelDummy.add(labelDummy4);
				panel.add(panelDummy);
				panel.add(Box.createRigidArea(new Dimension(0, 20)));
				i += 6;
			} catch(IndexOutOfBoundsException e) { break; }
			catch(NullPointerException e) { }
		}

		JLabel lblMyRating = new JLabel("My Rating");
		add(lblMyRating, "2, 12, left, default");

		rateBox = new JComboBox(rateVals);
		add(rateBox, "4, 12, fill, default");

		JLabel lblComments = new JLabel("Comments");
		add(lblComments, "2, 14, left, default");

		textField = new JTextField();
		add(textField, "4, 14, 5, 3, fill, default");
		textField.setColumns(10);

		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				c1.show(basePanel, "FOCUSCARD");
			}
		});
		add(btnCancel, "6, 18");

		JButton btnRateThisItem = new JButton("Rate This Item");
		btnRateThisItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event) 
			{
				String comment = textField.getText();
				String rateRaw = rateVals[rateBox.getSelectedIndex()];
				String rate = rateRaw.substring(0, 1);
				ExecuteQuery.rateItem(itemID, login.username, rate, comment);
			}
		});
		add(btnRateThisItem, "8, 18");
	}

}
